package com.besttone.search;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.aibang.open.client.AibangApi;
import com.besttone.search.model.BizInfo;
import com.besttone.search.model.ResultInfo;
import com.besttone.search.model.SearchParm;
import com.besttone.search.util.Constants;
import com.besttone.search.util.MD5Builder;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Parcelable.Creator;
import android.text.TextUtils;
import android.util.Log;

public class ServerProxy {
	private int i = 0;
	private List<Map<String, Object>> list;
	private Object obj;
	private AibangApi mAibang;
	private int total = 0; // 记录总数

	public List<Map<String, Object>> getAbData(SearchParm parm, int num) {
		mAibang = new AibangApi(Constants.abAppKey);
		// ResultInfo info = new ResultInfo();
		list = new LinkedList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			String rtnStr = mAibang.search(parm.getCity(), parm.getA(),
					parm.getLng(), parm.getLat(), parm.getQ(), parm.getCate(),
					parm.getRc(), parm.getAs(), parm.getFrom(), parm.getTo());
			if (rtnStr != null) {
				JSONObject resultJsonObject = new JSONObject(rtnStr);
				String indexNum = (String) resultJsonObject.get("index_num")
						.toString();
				String totalStr = (String) resultJsonObject.get("total")
						.toString();
				if (totalStr != null) {
					total = new Integer(totalStr);
				}
				String resultNum = (String) resultJsonObject.get("result_num")
						.toString();
				JSONObject bizJsonObject = resultJsonObject
						.getJSONObject("bizs");
				JSONArray bizs = bizJsonObject.getJSONArray("biz");
				for (int i = 0; i < bizs.length(); i++) {
					// BizInfo biz = new BizInfo();
					map = new HashMap<String, Object>();

					JSONObject temp = (JSONObject) bizs.get(i);
					String id = (String) temp.get("id");
					String name = (String) temp.get("name");
					String addr = (String) temp.get("addr");
					String tel = (String) temp.get("tel");
					String cate = (String) temp.get("cate");
					String rate = (String) temp.get("rate").toString();
					String cost = (String) temp.get("cost").toString();
					String desc = (String) temp.get("desc");
					String dist = (String) temp.get("dist").toString();
					String lng = (String) temp.get("lng").toString();
					String lat = (String) temp.get("lat").toString();
					String imgUrl = (String) temp.get("img_url");

					int index = -1;
					if (tel != null) {
						index = tel.indexOf(" ");
					}
					if (index > 0) {
						tel = tel.substring(0, index);
					}
					map.put("name", name);
					map.put("tel", tel);
					// map.put("price", "人均：￥" + cost);
					map.put("price", tel);
					map.put("addr", addr);
					// map.put("distance", "4.3km");
					map.put("tuan", false);
					map.put("promo", false);
					map.put("card", false);
					map.put("checkin", false);
					map.put("star", 1);
					// map.put("area", "长宁区");
					map.put("id", id);
					map.put("lng", lng);
					map.put("lat", lat);
					map.put("desc", desc);
					list.add(map);
				}
			}
		} catch (Exception e) {
			Log.v("获取数据出错", e.getMessage());
		}
		return list;
	}

	public void sendRequest(final ServerListener listener, final SearchParm parm) {
		final int num = i;
		 if (i < 20) {
		Thread thread = new Thread() {
			public void run() {
				try {
					this.sleep(1000);
				} catch (Exception e) {

				}
				
				list = getAbData(parm, num);
				if(list!=null && list.size()==0){
					list = createData(num);
				}
				listener.serverDataArrived(list, false);
//				if (i % 4 == 0) {
//					listener.serverDataArrived(list, true);
//				} else {
//					listener.serverDataArrived(list, false);
//				}

			}
		};
		thread.start();
		i++;
		 }

	}

	private List<Map<String, Object>> createData(int i) {
		List<Map<String, Object>> list = new LinkedList<Map<String, Object>>();

		if (i == 0) {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("name", "俏江南 (桃江路店)");
			map.put("tel", "021-64452581");
			map.put("price", "021-64452581");
			map.put("addr", "桃江路28号-1");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "150728169-420373360");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "真功夫(新世界店)");
			map.put("price", "021-63592593 021-63268655");
			map.put("addr", "南京西路2-68号上海新世界商厦B1层");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "151818041-419114288");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "三人行骨头王火锅(徐汇总店)");
			map.put("price", "021-64682289 021-64690312");
			map.put("addr", "徐汇区肇嘉浜路879号(近宛平南路)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "18704792-420703279");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "金钱豹国际海鲜美食百汇(中信泰富店)");
			map.put("price", "021-52929999");
			map.put("addr", "静安区南京西路1168号中信泰富广场4-5楼(江宁路与陕西北路间)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1806379349-950007391");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "港丽餐厅(正大店)");
			map.put("price", "021-50472877");
			map.put("addr", "浦东新区陆家嘴西路168号正大广场7楼08A号铺(近东方明珠)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1387918682-447708041");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "辛香汇(西藏中路店)");
			map.put("price", "400 100 1717");
			map.put("addr", "黄浦区金陵东路500号亚龙国际广场4楼(近西藏中路)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "286972930-428030981");
			list.add(map);
		}

		if (i == 1) {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("name", "渝信川菜(招商局店)");
			map.put("price", "021-52980438 021-52980439");
			map.put("addr", "静安区成都北路333号招商局广场3楼");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "2030953855-421723307");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "广州蕉叶(香港广场店)");
			map.put("price", "021-53833333");
			map.put("addr", "卢湾区淮海中路283号香港广场南楼4楼");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "435438091-443221985");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "新旺茶餐厅(无限度店)");
			map.put("price", "021-63756357");
			map.put("addr", "卢湾区淮海中路138号无限度广场1-2楼(近普安路)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "443135403-435628593");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "唐宫海鲜舫(浦东店)");
			map.put("price", "021-50988588 021-50988868");
			map.put("addr", "浦东新区陆家嘴西路168号正大广场6楼11-13号铺(近东方明珠)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "830916734-695252636");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "厚味香辣馆(天钥桥路店)");
			map.put("price", "021-64282777 021-64289979");
			map.put("addr", "徐汇区天钥桥路458号4楼腾飞大厦斜对面(南丹路斜土路间)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1951685590-409433008");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "保罗酒家");
			map.put("price", "021-62792827");
			map.put("addr", "静安区富民路271号(长乐路与富民路交叉口)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1821821941-944189343");
			list.add(map);
		}

		if (i == 2) {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("name", "翠华餐厅(富民路店)");
			map.put("price", "021-61701282");
			map.put("addr", "徐汇区富民路291号悟锦世纪大厦内(长乐路口)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "566269476-433539649");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "廊亦舫酒楼(正大店)");
			map.put("price", "021-50471266");
			map.put("addr", "浦东新区陆家嘴西路168号正大广场5楼(近银城西路)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "285568578-419793887");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "新利查西餐馆(广元路店)");
			map.put("price", "021-62828618");
			map.put("addr", "徐汇区广元路196号(与天平路交汇处)");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "981664471-703373224");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "789新概念火锅料理(人民广场店)");
			map.put("price", "021-63508182 021-63508183");
			map.put("addr", "黄浦区西藏中路180号高盛大厦4楼");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "971797399-704138360");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "汉泰东南亚风味餐厅(大宁店)");
			map.put("price", "021-33871718 021-33871797");
			map.put("addr", "闸北区共和新路1998号大宁国际商业广场11栋4楼");
			map.put("tuan", false);
			map.put("promo", true);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1227119929-421816522");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "西堤牛排(天钥桥店)");
			map.put("price", "021-54242365 021-54246727");
			map.put("addr", "徐汇区天钥桥路311号4层");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "559314900-431915356");
			list.add(map);
		}

		if (i == 3) {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("name", "祖祖餐厅(虹桥店)");
			map.put("price", "021-62957942");
			map.put("addr", "长宁区虹桥路1480号(近延安西路)");
			//map.put("distance", "5.4km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1523137859-1604581459");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "PANKOO釜山料理(来福士店)");
			map.put("price", "021-63403181");
			map.put("addr", "黄浦区西藏中路268号来福士广场6楼(近福州路)");
			//map.put("distance", "10km");
			map.put("tuan", false);
			map.put("promo", true);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			//map.put("area", "青浦区");
			map.put("id", "1655458956-1205406179");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "夏朵西餐厅(政民路店)");
			map.put("price", "021-65112276");
			map.put("addr", "杨浦区政民路301号(国定路口)");
			//map.put("distance", "8.1km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "285994818-420212555");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "揽香川菜馆(无限度店)");
			map.put("price", "021-63756771 021-63869577");
			map.put("addr", "卢湾区淮海中路138号无限度广场4楼(近普安路)");
			//map.put("distance", "7.4km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "2031092815-421434136");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "洋葱餐厅(龙之梦店)");
			map.put("price", "021-52398668 021-52399880");
			map.put("addr", "长宁路1018号龙之梦购物中心8楼");
			//map.put("distance", "10km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "828125422-695466856");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "021上海菜主题餐厅(353广场店)");
			map.put("price", "021-62777886 021-33313778");
			map.put("addr", "黄浦区南京东路353号353广场7楼(河南中路附近)");
			//map.put("distance", "3.7km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1093172496-1497486053");
			list.add(map);
		}

		if (i == 4) {
			Map<String, Object> map = new HashMap<String, Object>();

			map.put("name", "海底捞火锅(吴中路店)");
			map.put("price", "021-54223132 021-54220948");
			map.put("addr", "闵行区吴中路1100号(吴中路与万源路交叉口)");
			//map.put("distance", "2.4km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "984912647-703443416");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "穀屋");
			map.put("price", "021-50806230");
			map.put("addr", "浦东新区碧波路635号上海传奇1楼105号铺(近张江地铁站)");
			//map.put("distance", "5.1km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "151769209-419049614");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "望湘园(龙之梦店)");
			map.put("price", "021-33727097");
			map.put("addr", "长宁区长宁路1018号龙之梦购物中心蓝中庭5楼(近凯旋路)");
			//map.put("distance", "7.1km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "330470802-420212459");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "和记小菜(徐汇店)");
			map.put("price", "021-64879977");
			map.put("addr", "徐汇区南丹东路188号");
			//map.put("distance", "5.1km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "285637186-428031427");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "蛋蛋屋(龙之梦店)");
			map.put("price", "021-33727079");
			map.put("addr", "长宁区长宁路1018号龙之梦购物中心B2楼");
			//map.put("distance", "7.4km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "153398649-419176046");
			list.add(map);

			map = new HashMap<String, Object>();
			map.put("name", "小南国(正大广场店)");
			map.put("price", "4008209777");
			map.put("addr", "浦东新区陆家嘴西路168号正大广场9楼(近东方明珠)");
			//map.put("distance", "10km");
			map.put("tuan", false);
			map.put("promo", false);
			map.put("card", false);
			map.put("checkin", false);
			map.put("star", 1);
			map.put("id", "1093196480-1497486043");
			list.add(map);
		}
		return list;
	}

	public void getBizInfo(String city, String keyword) {
		try {
			// 获取XMLL
			String rtnXml = getBizXml(city, keyword);

			// 解析XML
			Log.v("返回XML", rtnXml);
		} catch (Exception e) {
			Log.v("XML解析出错", e.getMessage());
		}
	}

	public String getBizXml(String city, String keyword) throws Exception {
		// 返回XML
		String rtnXml = "";
		// http地址
		StringBuilder requestUrl = new StringBuilder(Constants.abServerAddress);
		String parm = "?app_key=" + Constants.abAppKey + "&city="
				+ URLEncoder.encode(city) + "&q=" + URLEncoder.encode(keyword);
		requestUrl.append(parm);
		String httpUrl = requestUrl.toString();
		Log.v("HTTP", httpUrl);
		// HttpGet连接对象
		HttpGet httpRequest = new HttpGet(httpUrl);
		// 取得HttpClient对象
		HttpClient httpclient = new DefaultHttpClient();
		// 请求HttpClient，取得HttpResponse
		HttpResponse httpResponse = httpclient.execute(httpRequest);
		// 请求成功
		if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
			// 取得返回的字符串
			rtnXml = EntityUtils.toString(httpResponse.getEntity()).trim();
			// rtnXml = new String(rtnXml.getBytes("UTF-8"),"GBK");
		}
		return rtnXml;
	}

}